package com.telenav.snowsweeper;


import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Process;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;

import com.telenav.snowsweeper.controller.GameController;
import com.telenav.snowsweeper.controller.GameLoader;
import com.telenav.snowsweeper.controller.Notifier;
import com.telenav.snowsweeper.net.ThreadPool;
import com.telenav.snowsweeper.renderer.Util;
import com.telenav.snowsweeper.util.ResourceUtil;


public class MainActivity extends Activity
{
	private GameController gameController;
    private boolean gameStarted = true;
    
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);
        
        init();
        UrlsOfServer.getInsttance().loadServerConfig(this);
        Button quitBt = (Button) this.findViewById(R.id.quit);
        gameController = new GameController(new GameLoader(this));
        quitBt.setOnClickListener(new OnClickListener()
        {
            public void onClick(View v)
            {
                ThreadPool.getInstance().stop();
                MainActivity.this.finish();
                Process.killProcess(Process.myPid());
            }
        });
        Button newGameBt = (Button) this.findViewById(R.id.start_game);
		newGameBt.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				gameStarted = true;
				gameController.startGame(1);
			}
		});
	}
    
    private void init() {
    	Util.init(this);
    	ResourceUtil.init(this);
    	Notifier.init(this);
    }
    
    @Override
    protected void onPause()
    {
        super.onPause();
        ThreadPool.getInstance().stop();
    }

    @Override
    protected void onResume()
    {
        ThreadPool.getInstance().start();
        super.onResume();
    }

	public boolean onCreateOptionsMenu(Menu menu) {
		if (!gameStarted) {
			getMenuInflater().inflate(R.menu.activity_main, menu);
		}
		return true;
	}

    public boolean onOptionsItemSelected(MenuItem item)
    {
        Intent ins = new Intent(MainActivity.this, SettingActivity.class);
        startActivity(ins);
        return true;
    }
}
